Conditions | 2 |
Paths | 128 |
Total Lines | 158 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | var Race = require('./Race').Race |
||
42 | function Future (resolver) { |
||
43 | var self = this |
||
44 | var status = Status.Pending |
||
45 | var identity |
||
46 | var propagation = null |
||
47 | var queue = [] |
||
48 | |||
49 | this.getValue = function () { return identity } |
||
50 | |||
51 | this.getStatus = function () { return status } |
||
52 | |||
53 | this.hasStatus = function (s) { return status === s } |
||
54 | |||
55 | this.isPending = function () { return self.hasStatus(Status.Pending) } |
||
56 | this.isFulfilled = function () { return self.hasStatus(Status.Fulfilled) } |
||
57 | this.isRejected = function () { return self.hasStatus(Status.Rejected) } |
||
58 | this.isResolved = function () { |
||
59 | return status === Status.Fulfilled || status === Status.Rejected |
||
60 | } |
||
61 | |||
62 | function schedulePropagation () { |
||
63 | // if already scheduled |
||
64 | if (propagation) { return } |
||
65 | // @see https://promisesaplus.com/#point-34 |
||
66 | // setTimeout is used because process.nextTick is not something one mocks |
||
67 | propagation = setTimeout(function () { |
||
68 | propagation = null |
||
69 | propagate() |
||
70 | }, 0) |
||
71 | } |
||
72 | |||
73 | function propagate () { |
||
74 | if (!self.isResolved()) { return } |
||
75 | var staged = queue |
||
76 | queue = [] |
||
77 | staged.forEach(function (callback) { |
||
78 | callback(status, identity) |
||
79 | }) |
||
80 | } |
||
81 | |||
82 | function setIdentity (nextStatus, value) { |
||
83 | if (self.isResolved()) { return } |
||
84 | status = nextStatus |
||
85 | identity = value |
||
86 | schedulePropagation() |
||
87 | } |
||
88 | |||
89 | var fulfill = setIdentity.bind(self, Status.Fulfilled) |
||
90 | var reject = setIdentity.bind(self, Status.Rejected) |
||
91 | |||
92 | function resolve (nextStatus, value) { |
||
93 | if (!self.isPending()) { return self } |
||
94 | extendedResolutionProcedure(nextStatus, value) |
||
95 | return self |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Resolves (fulfills) current instance with provided value |
||
100 | * |
||
101 | * @param {T} [value] |
||
102 | * @returns {Future.<T>} Current instance |
||
103 | */ |
||
104 | this.fulfill = resolve.bind(this, Status.Fulfilled) |
||
105 | |||
106 | this.resolve = this.fulfill |
||
107 | |||
108 | /** |
||
109 | * Rejects current instance with provided value |
||
110 | * |
||
111 | * @param {T} [value] |
||
112 | * @returns {Future.<T>} Current instance |
||
113 | */ |
||
114 | this.reject = resolve.bind(this, Status.Rejected) |
||
115 | |||
116 | if (resolver) { resolver(this.resolve, this.reject) } |
||
117 | |||
118 | /** |
||
119 | * @param x |
||
120 | */ |
||
121 | function promiseResolutionProcedure (x) { |
||
122 | if (self.isResolved()) { return } |
||
123 | status = Status.Resolving |
||
124 | if (x === self) { |
||
125 | var message = 'Can\'t resolve promise with itself' |
||
126 | return setIdentity(Status.Rejected, new TypeError(message)) |
||
127 | } |
||
128 | if (x instanceof Future && x.isResolved()) { |
||
129 | return extendedResolutionProcedure(x.getStatus(), x.getValue()) |
||
130 | } |
||
131 | if (!x || (typeof x !== 'function' && typeof x !== 'object')) { |
||
132 | return fulfill(x) |
||
133 | } |
||
134 | var race = new Race(1) |
||
135 | var resolvePromise = race.racer(promiseResolutionProcedure) |
||
136 | var rejectPromise = race.racer(reject) |
||
137 | try { |
||
138 | var then = x.then |
||
139 | if (typeof then !== 'function') { return fulfill(x) } |
||
140 | then.call(x, resolvePromise, rejectPromise) |
||
141 | } catch (e) { |
||
142 | rejectPromise(e) |
||
143 | } |
||
144 | } |
||
145 | |||
146 | function extendedResolutionProcedure (status, x) { |
||
147 | status === Status.Rejected ? reject(x) : promiseResolutionProcedure(x) |
||
148 | return self |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Standard then-implementation |
||
153 | * |
||
154 | * @param {Function} [onFulfill] |
||
155 | * @param {Function} [onReject] |
||
156 | * @returns {Future.<T>} |
||
157 | */ |
||
158 | this.then = function (onFulfill, onReject) { |
||
159 | if (typeof onFulfill !== 'function') { onFulfill = null } |
||
160 | if (typeof onReject !== 'function') { onReject = null } |
||
161 | if (!onFulfill && !onReject) { return self } |
||
162 | onReject = onReject || function (error) { throw error } |
||
163 | onFulfill = onFulfill || function (value) { return value } |
||
164 | var target = new Future() |
||
165 | var callback = function (status, value) { |
||
166 | var handler = status === Status.Fulfilled ? onFulfill : onReject |
||
167 | try { |
||
168 | target.resolve(handler(value)) |
||
169 | } catch (e) { |
||
170 | target.reject(e) |
||
171 | } |
||
172 | } |
||
173 | queue.push(callback) |
||
174 | schedulePropagation() |
||
175 | return target |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Attaches current promise to provided thenable, accepting it's eventual |
||
180 | * outcome. |
||
181 | * |
||
182 | * @param {Thenable} promise |
||
183 | * |
||
184 | * @return {Future} |
||
185 | */ |
||
186 | this.wrap = function (promise) { |
||
187 | promise.then(function (value) { |
||
188 | self.fulfill(value) |
||
189 | }, function (e) { |
||
190 | self.reject(e) |
||
191 | }) |
||
192 | return self |
||
193 | } |
||
194 | |||
195 | this.toString = function () { |
||
196 | var state = Status.name(status) |
||
197 | return 'Future <' + state + (identity ? ':' + identity : '') + '>' |
||
198 | } |
||
199 | } |
||
200 | |||
257 |